﻿namespace SourceCodeRepository.Web.Tests.Controllers.Language
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;
    using Moq;
    using Persistence.Repositories.Interfaces;
    using Web.Controllers.Home.Language;
    using Xunit;
    using Model.Entitites;

    public class LanguageControllerFacts
    {
        [Fact]
        public void Test_If_The_Language_Controller_Can_Get_All_Language()
        {
            var expectedLanguage = new Language { Id = 1, Description = "C#" };
            var controller = new LanguageController(this.GetMockedRepository(new List<Language> { expectedLanguage }));
            var list = (controller.ListLanguages(0) as ViewResult).ViewData.Model as IList<Language>;

            Assert.Equal(1, list.Count);
            Assert.Equal(expectedLanguage, list.First());
        }

        /// <summary>
        /// Gets the mocked repository.
        /// </summary>
        /// <param name="languages">The languages.</param>
        /// <returns></returns>
        private ILanguageRepository GetMockedRepository(IList<Language> languages)
        {
            var mockedRepository = new Mock<ILanguageRepository>();
            mockedRepository.Expect(o => o.GetAll(0)).Returns(languages);
            return mockedRepository.Object;
        }

        [Fact]
        public void Test_If_The_Controller_Can_Show_The_Default_View()
        {
            var expectedLanguage = new Language { Id = 1, Description = "C#" };
            var controller = new LanguageController(this.GetMockedRepository(new List<Language> { expectedLanguage }));

            var view = controller.ShowView() as ViewResult;
            Assert.Equal("ShowLanguages", view.ViewName);
        }
    }
}
